package com.migu.contacts.main;

import static com.migu.contacts.net.Service.API;

import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.BaseAdapter;
import com.frank.libandroid.CenterPopup;
import com.frank.libandroid.HttpCallback;
import com.frank.libandroid.butterknife.BindAdapter;
import com.frank.libandroid.butterknife.OnClick;
import com.lxj.xpopup.XPopup;
import com.migu.contacts.R;
import com.migu.contacts.bean.ActionBean;
import com.migu.contacts.bean.DetailBean;
import com.migu.contacts.bean.TimerBean;
import com.migu.contacts.databinding.ActivityEventDetailBinding;
import com.migu.contacts.databinding.ItemCreateTodoBinding;
import com.migu.contacts.databinding.PopTimerDeleteBinding;
import com.migu.contacts.net.ResultCallback;
import com.migu.contacts.util.PopUtil;
import com.migu.contacts.widget.Callback;
import com.migu.contacts.widget.Label2;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

public class EventDetailActivity extends BaseActivity<ActivityEventDetailBinding> {

    @BindAdapter(R.id.recyclerView1)
    BaseAdapter<ItemCreateTodoBinding, Label2.LabelBean> topAdapter = new BaseAdapter<ItemCreateTodoBinding, Label2.LabelBean>() {
        @OnClick(R.id.iv_arrow)
        public void iv_arrow(Label2.LabelBean bean, int position) {
            String str = null;
            if (position == 0) {
                str = actionBean.startTime;
            } else {
                str = actionBean.endTime;
            }
            PopUtil.showDateTimePicker(mActivity, str, new Callback<String>() {
                @Override
                public void onComplete(String s) {
                    bean.right = s;
                    if (position == 0) {
                        actionBean.startTime = s;
                    } else {
                        actionBean.endTime = s;
                    }
                    notifyDataSetChanged();
                }
            });
        }
    };
    @BindAdapter(R.id.recyclerView2)
    BaseAdapter<ItemCreateTodoBinding, Label2.LabelBean> midAdapter = new BaseAdapter<ItemCreateTodoBinding, Label2.LabelBean>() {
        @OnClick(R.id.iv_arrow)
        public void iv_arrow(Label2.LabelBean bean, int position) {
            if (position == 0) {
//                showPopNotice(bean);
            } else if (position == 1) {
//                showCyclePop(bean);
            } else if (position == 2) {
//                showImportancePop(bean);
            }
        }
    };
    ActionBean actionBean;
    DetailBean<ActionBean> detailBean;

    @Subscribe(sticky = true)
    public void onMessage(TimerBean bean) {
        EventBus.getDefault().removeStickyEvent(bean);
        API.getCalendarActionDetail(bean.id).watch(this, new HttpCallback<DetailBean<ActionBean>>() {
            @Override
            protected void onSuccess(DetailBean<ActionBean> todoBean) {
                ActionBean data = todoBean.data;
                detailBean = todoBean;
                mBinding.setBean(data);
                actionBean = data;
                topAdapter.getData().get(0).right = data.startTime;
                topAdapter.getData().get(1).right = data.endTime;
                topAdapter.notifyDataSetChanged();
                mBinding.setRaw(todoBean);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                super.onError(errorCode, errorMessage);
                finish();
            }
        });

    }

    @Override
    protected void initView() {
        super.initView();

    }

    @Override
    protected void initData() {
        super.initData();
        List<Label2.LabelBean> top = new ArrayList<>();
        top.add(new Label2.LabelBean("开始时间", "请选择开始时间", 0, true));
        top.add(new Label2.LabelBean("结束时间", "请选择结束时间", 0, true));
        topAdapter.setData(top);
        List<Label2.LabelBean> mid = new ArrayList<>();
        mid.add(new Label2.LabelBean("提醒", "不提醒", 0, true));
        mid.add(new Label2.LabelBean("重复", "无", 0, true));
        mid.add(new Label2.LabelBean("重要程度", "无", 0, true));
        midAdapter.setData(mid);
    }

    public void delete(View view) {
        if (detailBean == null) {
            return;
        }
        CenterPopup bottomPopup = new CenterPopup<PopTimerDeleteBinding>(mActivity) {
            @OnClick(R.id.confirm)
            public void confirm() {
                Log.d(TAG, "confirm() called");
                API.calendarDelete(detailBean.id).watch(mActivity, new HttpCallback<Boolean>() {
                    @Override
                    protected void onSuccess(Boolean aBoolean) {
                        if (aBoolean) {
                            ToastUtils.showShort("删除成功");
                            dismiss();
                            finish();
                        }
                    }
                });
            }

            @OnClick(R.id.quit)
            public void quit() {
                dismiss();
            }

        };
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
    }

    public void changeStatus(View view) {
        if (detailBean == null) {
            return;
        }
        int status = detailBean.status;
        if (status == 1) {
            status = 2;
        } else {
            status = 1;
        }
        int finalStatus = status;
        API.calendarChangeStatus(detailBean.id, status).enqueue(new ResultCallback<Boolean>() {
            @Override
            protected void onSuccess(Boolean aBoolean) {
                if (aBoolean) {
                    detailBean.status = finalStatus;
                    mBinding.setRaw(detailBean);
                }
            }
        });
    }

    public void save(View view) {
        API.calendarEdit(detailBean).enqueue(new ResultCallback<Boolean>() {
            @Override
            protected void onSuccess(Boolean aBoolean) {
                if (aBoolean) {
                    ToastUtils.showShort("保存成功");
                    finish();
                }
            }
        });
    }
}